package com.hzeport.bgs22emduty.inbound.api;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.LoginCompoentService;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.config.systemLog.OperLog;
import com.hzeport.bgs22emduty.entity.ParamDictionary;
import com.hzeport.bgs22emduty.entity.ScreenDutyEmergencyCall;
import com.hzeport.bgs22emduty.outbound.ParamDictionaryRepository;
import com.hzeport.bgs22emduty.outbound.ScreenDutyEmergencyCallRepository;
import com.hzeport.bgs22emduty.vo.base.PageVO;
import com.hzeport.bgs22emduty.common.base.BaseEntity;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/emergencyContactType")
@Api(value = "emergencyContactType", tags = "应急联络类型设置")
@Slf4j
public class EmergencyContactTypeController {

    private final ParamDictionaryRepository paramDictionaryRepository;

    private final LoginCompoentService loginCompoentService;

    private final ScreenDutyEmergencyCallRepository screenDutyEmergencyCallRepository;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public BaseResult<Page<ParamDictionary>> page(@RequestBody PageVO dto) {
        Page<ParamDictionary> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        UserInfoDetailVO userInfoDetailVO=loginCompoentService.getUserInfo();
        Page<ParamDictionary> dictionaryPage = paramDictionaryRepository.page(page, new LambdaQueryWrapper<ParamDictionary>()
                .eq(ParamDictionary::getRemarks,userInfoDetailVO.getCustomscode())
                .eq(ParamDictionary::getParamModule, "LLLX")
                .orderByDesc(BaseEntity::getCreateTime));
        return BaseResult.ok(dictionaryPage);
    }

    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("/del")
    @OperLog( systemName = "YJLL", description = "DEL")
    public BaseResult<String> del(@RequestParam("id") String id) {
        ParamDictionary paramDictionary=paramDictionaryRepository.getById(id);
        if(paramDictionary.getParamModule().equals("LLLX"))
        {
            List<ScreenDutyEmergencyCall> screenDutyEmergencyCalls=screenDutyEmergencyCallRepository.list(new LambdaQueryWrapper<ScreenDutyEmergencyCall>()
            .eq(ScreenDutyEmergencyCall::getTitle,paramDictionary.getParamCode()));

            if(CollectionUtil.isNotEmpty(screenDutyEmergencyCalls))
            {
                return BaseResult.failed(null,"该类型存在关联应急联络内容，不可删除！");
            }
        }
        paramDictionaryRepository.removeById(id);
        return BaseResult.ok(null,"删除成功");
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/saveOrUpdate")
    @OperLog( systemName = "YJLL", description = "SAVE")
    public BaseResult<String> save(@RequestBody ParamDictionary dto) {
        UserInfoDetailVO userInfoDetailVO=loginCompoentService.getUserInfo();
        if (userInfoDetailVO!=null){
            dto.setRemarks(userInfoDetailVO.getCustomscode());
        }
        if (dto.getId()==null){
            List<ParamDictionary> paramDictionaries = paramDictionaryRepository.list(new LambdaQueryWrapper<ParamDictionary>()
                    .eq(ParamDictionary::getParamModule, "LLLX")
                    .last("ORDER BY CAST(PARAM_CODE AS INT) desc "));
            if (CollectionUtils.isEmpty(paramDictionaries)){
                dto.setParamCode("1");
            }else {
                String value = String.valueOf(Integer.parseInt(paramDictionaries.get(0).getParamCode()) + 1);
                dto.setParamCode(value);
            }
            dto.setParamModule("LLLX");
            paramDictionaryRepository.save(dto);
        }else {
            ParamDictionary byId = paramDictionaryRepository.getById(dto.getId());
            if (byId != null){
                dto.setRecVersion(byId.getRecVersion());
                paramDictionaryRepository.updateById(dto);
            }

        }
        return BaseResult.ok(null,"操作成功");
    }

}
